package com.yuanlrc.school.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuanlrc.school.entity.DiscussionDB;
import com.yuanlrc.school.service.DiscussionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("discussion")
public class DiscussionController {

    @Autowired
    private DiscussionService discussionService;

    /**
     * 进入发布文章页面
     * @return
     */
    @RequestMapping("addDiscussion")
    public String addDiscussion() {
        return "view/discussion/addDiscussion";
    }

    /**
     * 进入讨论管理页面
     * @return
     */
    @RequestMapping("discussionControl")
    public String DiscussionControl() {
        return "view/discussion/discussionControl";
    }


    /**
     * 发布讨论
     */
    @RequestMapping("addingDiscussion")
    @ResponseBody
    public Object addingDiscussion(DiscussionDB discussionDB){
        int r = discussionService.addDiscussion(discussionDB);
        if(r==1){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 获取讨论列表
     */
    @RequestMapping("findallDiscussion")
    @ResponseBody
    public Object findallDiscussion(DiscussionDB discussionDB, Integer page, Integer limit){
        PageHelper.startPage(page, limit);
        List<DiscussionDB> listAll = discussionService.findAllDiscussion(discussionDB);
        PageInfo pageInfo = new PageInfo(listAll);
        Map<String, Object> newsData = new HashMap<String, Object>();
        //这是layui要求返回的json数据格式
        newsData.put("code", 0);
        newsData.put("msg", "");
        //将全部数据的条数作为count传给前台（一共多少条）
        newsData.put("count", pageInfo.getTotal());
        //将分页后的数据返回（每页要显示的数据）
        newsData.put("data", pageInfo.getList());
        return newsData;
    }

    /**
     * 删除讨论
     * */
    @RequestMapping("deleteDiscussion")
    @ResponseBody
    public Object deleteDiscussion(Integer did){
        int r = discussionService.deleteDiscussion(did);
        if(r==1){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 编辑讨论
     */
    @RequestMapping("editingDiscussion")
    @ResponseBody
    public Object editDiscussion(DiscussionDB discussionDB){
        int r = discussionService.editDiscussion(discussionDB);
        if(r==1){
            return true;
        }else{
            return false;
        }
    }



}
